﻿using System;
using CID.Execution.Controller.PackageRegistration;
using CID.Execution.Support.PackageRegistration;
using NailsFramework.IoC;
using NUnit.Framework;

namespace CID.Execution.Controller.Tests.PackageRegistration
{
    [TestFixture]
    class WebPackageRegistrationServiceTests : BaseControllerTests
    {
        private WebPackageRegistrationService webPackageRegistrationService;

        public override void SetUp()
        {
            base.SetUp();

            PackageRegistratorStub.CleanFlags();
            webPackageRegistrationService = new WebPackageRegistrationService();
        }

        protected override void ConfigureNails(NailsFramework.Config.INailsConfigurator nails)
        {
            nails.IoC.Lemming<PackageRegistrator>(x => x.Ignore())
                .IoC.Lemming<PackageRegistratorStub>();
            base.ConfigureNails(nails);
        }

        [Test]
        public void TestRegisterPackageByIDs_ShouldCallIPackageRegistratorRegisterPackageByIDs()
        {
            webPackageRegistrationService.RegisterPackageByIDs(0, 0, 0, 0, null, null);

            Assert.IsTrue(PackageRegistratorStub.RegisterPackageByIdsCalled);
            Assert.IsFalse(PackageRegistratorStub.RegisterPackageByNamesCalled);
        }

        [Test]
        public void TestRegisterPackageByNames_ShouldCallIPackageRegistratorRegisterPackageByNames()
        {
            webPackageRegistrationService.RegisterPackageByNames(String.Empty, String.Empty, String.Empty, String.Empty, null, null);

            Assert.IsTrue(PackageRegistratorStub.RegisterPackageByNamesCalled);
            Assert.IsFalse(PackageRegistratorStub.RegisterPackageByIdsCalled);
        }
    }

    class PackageRegistratorStub : IPackageRegistrator
    {
        public static bool RegisterPackageByIdsCalled { get; private set; }
        
        public static bool RegisterPackageByNamesCalled { get; private set; }

        public static void CleanFlags()
        {
            RegisterPackageByIdsCalled = RegisterPackageByNamesCalled = false;
        }


        public void RegisterPackage(int componentId, int versionId, int packageTypeId, int responsibleId, byte[] content, string comments)
        {
            RegisterPackageByIdsCalled = true;
        }

        public void RegisterPackage(string componentName, string versionName, string packageTypeName, string responsibleUserName, byte[] content, string comments)
        {
            RegisterPackageByNamesCalled = true;
        }
    }
}
